export default function toListReducer(state = {
    todolist: [{
            id: 1,
            title: "吃饭",
            createTime: '2021/12/23',
            oldTime: true,
            done: false,
            children:[{
                id:'1-1',
                title:'买米',
                done:false
            },{
                id:'1-2',
                title:'洗米',
                done:false
            },{
                id:'1-3',
                title:'蒸米',
                done:false
            }]
        },
        {
            id: 2,
            title: "睡觉",
            createTime: '2021/12/24',
            oldTime: false,
            done: false,
            children:[]
        },
        {
            id: 3,
            title: "敲代码",
            createTime: '2021/12/24',
            oldTime: false,
            done: true,
            children:[]
        },
    ]
}, action) {
    switch (action.type) {
        // 匹配todo
        case "updateTodoList":
            const array = state.todolist.map(item => {
                if (item.id == action.payload) {
                    item.done = !item.done

                }
                return item
            })
            console.log(state);
            return {
                ...state,
                todolist: array
            }
        case "addTodoList":
            const {
                todolist
            } = state
            todolist.push(action.payload)
            return {
                ...state,
                todolist: [
                    ...todolist
                ]
            }

        case "updateOldTime":
              const newarray = state.todolist.map(item => {
                  if (item.createTime != action.payload) {
                      item.oldTime = true
                  }
                  return item
              })
              return {
                  ...state,
                  todolist: [
                      ...newarray
                    ]
                }
        case "mapoldtotoday":
             const oldTonewarray = state.todolist.map(item => {
                 if (item.createTime != action.payload) {
                     item.createTime = action.payload
                     item.oldTime = false
                     item.done = false
                 }

                 return item
             })
             console.log('new', oldTonewarray);
        case "updateTitle":
            const newtitleArray = state.todolist.map(item => {
                if (item.id == action.payload.id) {
                    item.title = action.payload.title
                }

                return item
            })

            return {
                ...state,
                todolist: [
                    ...newtitleArray
                ]
            }
        //添加细分步骤
        case "addstep":
            const newstepArray = state.todolist.map(item => {
                if (item.id == action.payload.id) {
                    item.children.push(action.payload.step)
                }

                return item
            })

            return {
                ...state,
                todolist: [
                    ...newstepArray
                ]
            }

        
        case "delchildtask":
            const newchildtaskArray = state.todolist.map(item => {
                if (item.id == action.payload.id) {
                    item.children = item.children.filter(item=>{
                        if(item.id!=action.payload.childid){
                                return true
                        }
                    })
                }
                return item
            })
            console.log('删除后的仓库',newchildtaskArray);

            return {
                ...state,
                todolist: [
                    ...newchildtaskArray
                ]
            }

        case "updatechilddone":
            const changeChildDone = state.todolist.map(item => {
                if (item.id == action.payload.id) {
                    item.children = item.children.map(item=>{
                        if(item.id==action.payload.childid){
                            item.done=action.payload.done
                        }
                        return item
                    })
                }

                return item
            })
            console.log('仓库状态孩子',changeChildDone);
            return {
                ...state,
                todolist: [
                    ...changeChildDone
                ]
            }
        case "updatechildtitle":
            const changeChildtitle = state.todolist.map(item => {
                if (item.id == action.payload.id) {
                    item.children = item.children.map(item=>{
                        if(item.id==action.payload.childid){
                            item.title=action.payload.title
                        }
                        return item
                    })
                }

                return item
            })
            console.log('仓库状态孩子',changeChildtitle);
            return {
                ...state,
                todolist: [
                    ...changeChildtitle
                ]
            } 

        default:
            return state
    }
}